//
//  HOTNickLabel.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-10-13.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTNickLabel.h"
#import "HOTProfileReviewViewController.h"
#import "HOTCostPopupHandler.h"

@interface HOTNickLabel () {
    
    BOOL isTouch;
}
@end
@implementation HOTNickLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)drawRect:(CGRect)rect {
    
    self.userInteractionEnabled = YES;
    [super drawRect:rect];
}

-(void)awakeFromNib {
    
    self.userInteractionEnabled = YES;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
//    NSLog(@"touch begin");
    isTouch = YES;
    
//    HOTProfileReviewViewController *prvc = [[HOTProfileReviewViewController alloc] initWithNibName:@"HOTProfileReviewViewController" bundle:nil userInfo:nil targetUserId:self.user_id];
//    [[self viewController].navigationController pushViewController:prvc animated:YES];
}

//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
//    NSLog(@"touch end");
//    UITouch *touch = (UITouch *)[touches anyObject];
//    
//    CGPoint point = [touch locationInView:self];
//    
//    if (point.x >0 && point.y >0 && [HOTUtils isNotNullOrEmpty:self.user_id]) {
//        
//        
//        HOTProfileReviewViewController *prvc = [[HOTProfileReviewViewController alloc] initWithNibName:@"HOTProfileReviewViewController" bundle:nil userInfo:nil targetUserId:self.user_id];
//        [[self viewController].navigationController pushViewController:prvc animated:YES];
//    }
    
//    isTouch = NO;
//}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
    if ([self checkValid]) {
        return;
    }
    
//    NSLog(@"touch cancel");
    UITouch *touch = (UITouch *)[touches anyObject];
    
    CGPoint point = [touch locationInView:self];
    
    if (point.x >0 && point.y >0 && [HOTUtils isNotNullOrEmpty:self.user_id]) {
        
        __unsafe_unretained HOTNickLabel *tempObj = self;
        [[HOTCostPopupHandler costPopupHandler] showWithPopupStyle:HOTCostPopupStyle1
                                                          userInfo:nil
                                                            cancel:nil confirm:^{
            
            HOTProfileReviewViewController *prvc = [[HOTProfileReviewViewController alloc] initWithNibName:@"HOTProfileReviewViewController" bundle:nil userInfo:nil targetUserId:tempObj.user_id];
            [[tempObj viewController].navigationController pushViewController:prvc animated:YES];
        }];
        
    }
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    
    if ([self checkValid]) {
        return;
    }
//    NSLog(@"touch cancel");
    UITouch *touch = (UITouch *)[touches anyObject];

    CGPoint point = [touch locationInView:self];

    if (point.x >0 && point.y >0 && [HOTUtils isNotNullOrEmpty:self.user_id]) {

        __unsafe_unretained HOTNickLabel *tempObj = self;
        [[HOTCostPopupHandler costPopupHandler] showWithPopupStyle:HOTCostPopupStyle1
                                                          userInfo:nil
                                                            cancel:nil confirm:^{
            
            HOTProfileReviewViewController *prvc = [[HOTProfileReviewViewController alloc] initWithNibName:@"HOTProfileReviewViewController" bundle:nil userInfo:nil targetUserId:tempObj.user_id];
            [[tempObj viewController].navigationController pushViewController:prvc animated:YES];
        }];
        
    }
}

-(BOOL)checkValid {
    
    HOTUserInfo *userInfo = [HOTUserInfo defaultUser];
    if ((userInfo.userId && [userInfo.userId isEqualToString:self.user_id]) || ![userInfo isLogined]) {
        
        return YES;
    }
    
    return NO;
}


-(UIViewController *)viewController {
    
    UIResponder *nexRes=[self nextResponder];

    if (!self.targetVcClass) {
        
        self.targetVcClass  = [UIViewController class];
    }
    
    do {
        //判读当前的响应者是否UIViewController
        if ([nexRes isKindOfClass:[self.targetVcClass class]]) {
            //是否直接处理
            return  (UIViewController*)nexRes;
        }else{
            //否则继续寻找
            nexRes=[nexRes nextResponder];
            
        }
    } while (nexRes!=nil);
    
    return nil;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
